package demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class DistanceAndLabel implements Writable {
    private double distance;
    private String label; //性别

    /**
     * 先读取距离，再读取类别
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.distance = in.readDouble();
        this.label = in.readUTF();

    }

    /**
     * 先把distnce写入out输出流
     * 再把label写入out输出流
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeDouble(distance);
        out.writeUTF(label);

    }

    /**
     * 使用空格将距离和类别连接成字符串
     */
    @Override
    public String toString() {
        return this.distance + "," + this.label;
    }
}
